package com.yoda.mall.mysql.api;

import com.alibaba.nacos.common.utils.Objects;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoda.mall.api.ProductApi;
import com.yoda.mall.model.dto.ProductDetailDto;
import com.yoda.mall.model.dto.ProductDto;
import com.yoda.mall.model.entity.pms.*;
import com.yoda.mall.model.entity.sms.HomeNewProduct;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.model.vo.ProductDetailVo;
import com.yoda.mall.mysql.mapper.*;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author shawn
 * @date 2022年 12月 16日 15:55
 */
@DubboService
public class ProductApiImpl implements ProductApi {

    @Resource
    private ProductMapper productMapper;

    @Resource
    private ProductLadderMapper productLadderMapper;

    @Resource
    private ProductFullReductionMapper productFullReductionMapper;

    @Resource
    private MemberPriceMapper memberPriceMapper;


    @Resource
    private SkuStockMapper skuStockMapper;

    @Override
    public PageResult<Product> selectByPage(ProductDto dto) {
        Integer page = dto.getPageNum();
        Integer pageSize = dto.getPageSize();
        String keyword = dto.getKeyword();
        String productSn = dto.getProductSn();
        Long productCategoryId = dto.getProductCategoryId();
        Integer publishStatus = dto.getPublishStatus();
        Integer verifyStatus = dto.getVerifyStatus();
        Long brandId = dto.getBrandId();
        Page<Product> productPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Product> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(keyword),Product::getName,keyword);
        wrapper.like(StringUtils.isNotEmpty(productSn),Product::getProductSn,productSn);
        wrapper.eq(Objects.nonNull(productCategoryId),Product::getProductCategoryId,productCategoryId);
        wrapper.eq(Objects.nonNull(publishStatus),Product::getPublishStatus,publishStatus);
        wrapper.eq(Objects.nonNull(verifyStatus),Product::getVerifyStatus,verifyStatus);
        wrapper.eq(Objects.nonNull(brandId),Product::getBrandId,brandId);
        //逻辑删除
        wrapper.eq(Product::getDeleteStatus,0);
        wrapper.orderByDesc(Product::getSort);
        productMapper.selectPage(productPage, wrapper);
        return new PageResult<>(page, pageSize, (int) productPage.getTotal(), productPage.getRecords());
    }

    @Override
    public Product getProductById(Long productId) {
        return productMapper.selectById(productId);
    }

    @Override
    public ProductDetailVo selectProductDetailById(Long productId) {
        //1.根据id查询商品
        Product product = productMapper.selectById(productId);
        //2.根据产品id查询产品阶段价格表
        LambdaQueryWrapper<ProductLadder> ladderWrapper = new LambdaQueryWrapper<>();
        ladderWrapper.eq(ProductLadder::getProductId,productId);
        List<ProductLadder> productLadderList = productLadderMapper.selectList(ladderWrapper);
        //3.根据产品id查询满减表
        LambdaQueryWrapper<ProductFullReduction> reductionWrapper = new LambdaQueryWrapper<>();
        reductionWrapper.eq(ProductFullReduction::getProductId,productId);
        List<ProductFullReduction> productFullReductionList = productFullReductionMapper.selectList(reductionWrapper);
        //4.根据产品id查询价格表
        LambdaQueryWrapper<MemberPrice> priceWrapper = new LambdaQueryWrapper<>();
        priceWrapper.eq(MemberPrice::getProductId,productId);
        List<MemberPrice> memberPriceList = memberPriceMapper.selectList(priceWrapper);
        //5. 根据产品id查询库存表
        LambdaQueryWrapper<SkuStock> stockWrapper = new LambdaQueryWrapper<>();
        stockWrapper.eq(SkuStock::getProductId,productId);
        List<SkuStock> skuStockList = skuStockMapper.selectList(stockWrapper);
        //构造对象返回
        ProductDetailVo vo = new ProductDetailVo();
        BeanUtils.copyProperties(product,vo);
        vo.setProductLadderList(productLadderList);
        vo.setMemberPriceList(memberPriceList);
        vo.setProductFullReductionList(productFullReductionList);
        vo.setSkuStockList(skuStockList);
        return vo;
    }

    @Override
    @Transactional
    public Integer createProduct(ProductDetailDto productParam) {
        Product product = new Product();
        BeanUtils.copyProperties(productParam,product);
        //1.保存商品
        int count1 = productMapper.insert(product);
        //2.保存阶梯价格表
        List<ProductLadder> productLadderList = productParam.getProductLadderList().stream().peek(s -> s.setProductId(product.getId())).collect(Collectors.toList());
        Integer count2 = productLadderMapper.saveBatch(productLadderList);
        //3.保存满减数据
        List<ProductFullReduction> productFullReductionList = productParam.getProductFullReductionList().stream().peek(s -> s.setProductId(product.getId())).collect(Collectors.toList());
        Integer count3 = productFullReductionMapper.saveBatch(productFullReductionList);
        //4.保存价格数据
        List<MemberPrice> memberPriceList = productParam.getMemberPriceList().stream().peek(s -> s.setProductId(product.getId())).collect(Collectors.toList());
        Integer count4 = memberPriceMapper.saveBatch(memberPriceList);
        //5.保存库存数据
        List<SkuStock> skuStockList = productParam.getSkuStockList().stream().peek(s -> s.setProductId(product.getId())).collect(Collectors.toList());
        Integer count5 = skuStockMapper.saveBatch(skuStockList);
        return count1+count2+count3+count4+count5;
    }

    @Override
    @Transactional
    public Integer updateListById(List<Product> products) {

        for (Product product : products) {
            productMapper.updateById(product);
        }
        return products.size();
    }

    @Override
    @Transactional
    public Integer updateProduct(ProductDetailDto productDetail) {
        //构造产品对象
        Product product = new Product();
        BeanUtils.copyProperties(productDetail,product);
        int count1 = productMapper.updateById(product);
        //构造阶梯价格表
        List<ProductLadder> productLadderList = productDetail.getProductLadderList();
        //3.构造满减数据
        List<ProductFullReduction> productFullReductionList = productDetail.getProductFullReductionList();
        //4.构造价格数据
        List<MemberPrice> memberPriceList = productDetail.getMemberPriceList();
        //5.构造库存数据
        List<SkuStock> skuStockList = productDetail.getSkuStockList();
        //批量更新

        for (ProductFullReduction productFullReduction : productFullReductionList) {
                productFullReductionMapper.updateById(productFullReduction);
        }
        int count2 = productFullReductionList.size();
        //批量更新

        for (MemberPrice memberPrice : memberPriceList) {
            memberPriceMapper.updateById(memberPrice);
        }
        int count3 = memberPriceList.size();
        //批量更新

        for (SkuStock skuStock : skuStockList) {
            skuStockMapper.updateById(skuStock);
        }
        int count4 = skuStockList.size();
        //批量更新

        for (ProductLadder productLadder : productLadderList) {
                productLadderMapper.updateById(productLadder);
        }
        int count5 = productLadderList.size();
        return count1+count2+count3+count4+count5;
    }


}
